<?php

/**
 * charter actions.
 *
 * @package    Moncada
 * @subpackage charter
 * @author     Federico Motta - Andrea Burza
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class charterActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	$culture=$this->getUser()->getCulture();
    //$this->charters = Doctrine_Core::getTable('Charter')->getAll($culture);
	$this->pager = new sfDoctrinePager(
							'Charter',
							16
							);
	$this->pager->setQuery(Doctrine_Query::create()
				->from('Charter a')
				->leftJoin('a.Tipo b')
			    ->leftJoin('a.Translation t')
			    ->leftJoin('b.Translation c')
				->andWhere('t.lang = ?', $culture )
						);
	$this->pager->setPage($request->getParameter('page', 1));
	$this->pager->init();
	$this->charters = $this->pager->getResults();
  }

  public function executeRedirect(sfWebRequest $request){
	$this->charter = Doctrine_Core::getTable('Charter')->find(array($request->getParameter('id')));
	$this->redirect('charter/show/?nome_slug='.$this->charter->getNomeSlug().'&id='.$this->charter->getId());  
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->charter = $this->getRoute()->getObject();
    $this->forward404Unless($this->charter);
  }
  public function executeShowimage(sfWebRequest $request)
  {
	$this->img=$request->getParameter('img');
    $this->charter = Doctrine_Core::getTable('Charter')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->charter);
  }

  public function executeCerca(sfWebRequest $request){
	$culture=$this->getUser()->getCulture();
	$this->charters = Doctrine_Core::getTable('Charter')->getAll($culture); 
	$this->chart = Doctrine_Core::getTable('Messaggi')->getSelected(5,$culture);
	  }
	  
  public function executeResults(sfWebRequest $request){
	$culture=$this->getUser()->getCulture();
	$this->charters = Doctrine_Core::getTable('Charter')->getSearch($request,$culture);
	  }
	  
  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CharterForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new CharterForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($charter = Doctrine_Core::getTable('Charter')->find(array($request->getParameter('id'))), sprintf('Object charter does not exist (%s).', $request->getParameter('id')));
    $this->form = new CharterForm($charter);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($charter = Doctrine_Core::getTable('Charter')->find(array($request->getParameter('id'))), sprintf('Object charter does not exist (%s).', $request->getParameter('id')));
    $this->form = new CharterForm($charter);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($charter = Doctrine_Core::getTable('Charter')->find(array($request->getParameter('id'))), sprintf('Object charter does not exist (%s).', $request->getParameter('id')));
    $charter->delete();

    $this->redirect('charter/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $charter = $form->save();

      $this->redirect('charter/edit?id='.$charter->getId());
    }
  }
}
